package cn.fotoit.product.system.controller;

import cn.fotoit.product.system.entity.Permission;
import cn.fotoit.product.system.service.PermissionService;
import cn.fotoit.product.util.LogInfo;
import cn.fotoit.product.util.ReturnBean;
import io.swagger.annotations.Api;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api(value = "资源接口", tags = "系统-资源相关接口，资源添加，修改，删除，查询")
@RestController
@CrossOrigin
@RequestMapping("permission")
public class PermissionController {

    @Autowired
    private PermissionService permissionService;

    @PostMapping("add")
    @LogInfo(info = "资源添加", saveRequestData = true, saveResponseData = true)
    @RequiresPermissions("system_permission_add")
    public ReturnBean add(
            @RequestParam String name,
            @RequestParam String permissionKey,
            @RequestParam String remark,
            String url,
            @RequestParam(required = false, defaultValue = "false") Boolean canChange) {
        Permission permission = new Permission();
        permission.setName(name);
        permission.setPermissionKey(permissionKey);
        permission.setRemark(remark);
        permission.setUrl(url);
        permission.setCanChange(canChange);
        permissionService.add(permission);
        return ReturnBean.success("添加成功");
    }


    @PostMapping("updateNotNull")
    @LogInfo(info = "资源修改", saveRequestData = true, saveResponseData = true)
    @RequiresPermissions("system_permission_update")
    public ReturnBean updateNotNull(
            @RequestParam Integer id,
            String name,
            String permissionKey,
            String remark,
            String url,
            Boolean canChange) {
        Permission permission = new Permission();
        permission.setId(id);
        permission.setName(name);
        permission.setPermissionKey(permissionKey);
        permission.setRemark(remark);
        permission.setUrl(url);
        permission.setCanChange(canChange);
        permissionService.updateNotNull(permission);
        return ReturnBean.success("修改成功");
    }

    @PostMapping("deleteById")
    @LogInfo(info = "根据id删除资源", saveRequestData = true, saveResponseData = true)
    @RequiresPermissions("system_permission_delete")
    public ReturnBean deleteById(@RequestParam Integer id) {
        permissionService.deleteById(id);
        return ReturnBean.success("删除成功");
    }

    @PostMapping("findAll")
    @RequiresPermissions("system_permission_list")
    public ReturnBean findAll(
            @RequestParam(required = false, defaultValue = "1") Integer page,
            @RequestParam(required = false, defaultValue = "15") Integer size) {
        return ReturnBean.success("查询成功", permissionService.findAll(page, size));
    }

    @PostMapping("findById")
    @RequiresPermissions("system_permission_list")
    public ReturnBean findById(
            @RequestParam Integer id) {
        return ReturnBean.success("查询成功", permissionService.findById(id));
    }


}
